package com.xbongbong.workflow.service.field.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.enums.ComboTypeEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.ProFormHelp;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.util.ItemUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.FilterCondition;
import com.xbongbong.workflow.pojo.WorkflowRulePojo;
import com.xbongbong.workflow.service.field.AbstractWorkflowConditionFieldService;
import com.xbongbong.workflow.service.field.WorkflowConditionFieldService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/06/07 14:03
 */
@Service("workflowConditionComboServiceImpl")
public class WorkflowConditionComboServiceImpl extends AbstractWorkflowConditionFieldService implements WorkflowConditionFieldService {

    @Resource
    private ProFormHelp proFormHelp;

    @Override
    public List<Integer> fieldType() {
        return Arrays.asList(FieldTypeEnum.COMBO.getType(), FieldTypeEnum.RADIO_GROUP.getType());
    }


    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowNodeEntity workflowEntity, FilterCondition workflowConditionPojo) throws XbbException {
        Object object = workflowConditionPojo.getValue();
        String corpid = workflowEntity.getCorpid();
        if (object instanceof String) {
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, workflowEntity.getBusinessType(), corpid, workflowEntity.getFormId());
                targetField.setItems(itemPoJos);
                object = ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
            }
        } else if (object instanceof Map || object instanceof ItemPoJo) {
            if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType())) {
                ItemPoJo itemPoJo = JSON.parseObject(JSON.toJSONString(object), ItemPoJo.class);
                object = itemPoJo.getValue();
            }
        }
        return object;
    }


    @Override
    public Object handlerThisFormField(PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField,
                                       WorkflowNodeEntity workflowNodeEntity, FilterCondition filterCondition, WorkflowRulePojo workflowRulePojo) throws XbbException {
        Object object;
        String corpid = workflowNodeEntity.getCorpid();
        // 根据value获取text
        // 1、如果本表单字段是下拉框、下拉复选、单选按钮、复选按钮，需要先根据value取出text，放入data中；
        // 2、在下方取出data的text，然后拿到对应的value，之后在根据value去查询对应的数据；
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), fieldAttrEntity.getComboType())) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(fieldAttrEntity, filterCondition.getTargetBusinessType(), corpid, filterCondition.getTargetFormId());
            fieldAttrEntity.setItems(itemPoJos);
            object = ItemUtil.getCustomItemList(fieldAttrEntity, data);
        } else {
            object = ItemUtil.getCustomItemList(fieldAttrEntity, data);
        }
        return object;
    }

    @Override
    public Object handlerTargetFormField(Object object, PaasFormDataEntityExt paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, WorkflowRulePojo workflowRulePojo, FilterCondition filterCondition) throws XbbException {
        String corpid = workflowNodeEntity.getCorpid();
        Integer businessType = workflowNodeEntity.getBusinessType();
        if (Objects.equals(ComboTypeEnum.CUSTOM.getType(), targetField.getComboType()) && Objects.nonNull(object)) {
            List<ItemPoJo> itemPoJos = proFormHelp.getItem(targetField, businessType, corpid, workflowNodeEntity.getFormId());
            targetField.setItems(itemPoJos);
            return ItemUtil.getCustomItemValue(targetField, String.valueOf(object));
        }else {
            return object;
        }
    }
}
